import tkinter as tk
from tkinter import ttk
from tkinter import simpledialog
from tkinter import ttk, filedialog, messagebox
import csv
from datetime import datetime
root = tk.Tk()

def load_data(file_path):
    with open(file_path, newline='') as file:
        reader = csv.reader(file)
        for row in reader:
            tree.insert('', 'end', values=row)

def save_data():
    file_path = filedialog.asksaveasfilename(defaultextension=".csv")
    if not file_path:
        return
    with open(file_path, 'w', newline='') as file:
        writer = csv.writer(file)
        for child_id in tree.get_children():
            row = tree.item(child_id)['values']
            writer.writerow(row)
    messagebox.showinfo("Info", "Data saved successfully")
root.title("To-Do_List")
    # 创建 Treeview 控件，并指定列名
tree = ttk.Treeview(root, columns=("Priority", "Content", "Begin Time","End Time"), show="headings")
    
    # 设置每列的标题
tree.heading("Priority", text="Priority")
tree.heading("Content", text="Content")
tree.heading("Begin Time", text="Begin Time")
tree.heading("End Time", text="End Time")
    # 设置每列的宽度
tree.column("Priority", width=50)
tree.column("Content", width=100)
tree.column("Begin Time", width=100)
tree.column("End Time",width=100)
    # 添加滚动条(高级功能，抄一抄即可)
vsb = ttk.Scrollbar(root, orient="vertical", command=tree.yview)
hsb = ttk.Scrollbar(root, orient="horizontal", command=tree.xview)
tree.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)
    # 将滚动条放置到窗口中
vsb.pack(side='right', fill='y')
hsb.pack(side='bottom', fill='x')

    # 将 Treeview 放置到主窗口中
tree.pack(expand=True, fill=tk.BOTH)
def add_new_data():
    user_input=simpledialog.askstring("Input", "Please enter some data:")
    tree.insert("","end",values=user_input)



load_button = tk.Button(root, text="Load Data", command=lambda: load_data(filedialog.askopenfilename()))
load_button.pack(side=tk.LEFT, padx=5, pady=5)

# 添加按钮以保存数据
save_button = tk.Button(root, text="Save Data", command=save_data)
save_button.pack(side=tk.RIGHT, padx=5, pady=5)

# 创建一个按钮
add_button = tk.Button(root, text="Add New Data", command=add_new_data)
add_button.pack(pady=10)
def button1_touch():#遍历获取所有的索引（区分为一个被选中的或者是没有被选中）
    selected_indices=tree.selection()
    if selected_indices:
        for index in reversed(selected_indices):#反向删除
            tree.delete(index)#除去这个东西
remove_button=tk.Button(root,text="Remove Data",command=button1_touch)
remove_button.pack()

def on_select(event):
    # 获取选中的行
    selected_item = tree.selection()
    if selected_item:  # 确保有选中的行
        item_id = selected_item[0]  # 通常只会选中一行，所以取第一个
        # 获取选中行的所有数据
        item_data = tree.item(item_id)
        # 提取特定列的数据
        priority = item_data['values'][0]
        event = item_data['values'][1]
        begin_time = item_data['values'][2]
        end_time=item_data['values'][3]
        # 获取当前时间
        now = datetime.now()
        hour =now.hour
        minute=now.minute   
        current_time=f"{hour}:{minute}"
        print(current_time)
        Begin=datetime.strptime(begin_time,"%H:%M")
        End=datetime.strptime(end_time,"%H:%M")
        current=datetime.strptime(current_time,"%H:%M")
        if Begin > current:
            print(f"事件 {item_data['values'][1]} 尚未发生。")
        elif End < current:
            print(f"事件 {item_data['values'][1]} 已经发生。")
        elif End>current and Begin<current:
            print(f"事件 {item_data['values'][1]} 正在发生。")

# 绑定选择事件
tree.bind('<<TreeviewSelect>>', on_select)

root.mainloop()